// map.js
import qqMap from '../../../utils/qqMap'
let repairId = 0
Page({
  data: {
    dialogVisible: false,
    latitude: 40.060539,
    longitude: 116.343847,
    markers: [
      // 云趣园
      {
        id: 1,
        latitude: 40.086757,
        longitude: 116.328634,
        width: 24,
        height: 30
      },
      // 育知路
      {
        id: 2,
        latitude: 40.08346500000002,
        longitude: 116.33293800000011,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40
      },
      {
        id: 3,
        latitude: 40.060539,
        longitude: 116.343847,
        width: 24,
        height: 30
      }
    ],
    polyline: [{
      points: [ 
        { latitude: 40.08346500000002, longitude: 116.33293800000011 }, // 云趣园
				{ latitude: 40.086757, longitude: 116.328634 }, // 育知路
      ],
      color: '#FF0000DD',
      width: 4
    }]
  },
  // 确认提示框关闭时触发
  async dialogClose(ev) {
    console.log(ev.detail)
    if(ev.detail === 'confirm'){
      const{code} = await wx.http.put(`/cancel/repaire/${repairId}`)
      if(code !== 10000) return wx.utils.toast()
      wx.navigateBack({})
    }
  },
  onLoad({id}){
    repairId = id
    this.getRepairDetail(id)
    this.createPolyline()
  },
  async getRepairDetail(id){
    const {code,data} = await wx.http.get(`/repair/${id}`)
    if(code !== 10000) return wx.utils.toast()
    this.setData({
      ...data
    })
  },
  createPolyline(){
    qqMap.direction({
      mode:'bicycling',
      from:'40.060539,116.343847',// 传智播客
      to: '40.086757,116.328634',//（示例）云趣园1区
      success:(res) => {
        console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        this.setData({
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      }
    })
  },
  openDialogLayer(){
    this.setData({
      dialogVisible:true
    })
  },
  goEditRepair(){
    wx.navigateTo({
      url:'/repair_pkg/pages/form/index?id=' + repairId
    })
  }
})
 